<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>cors</title>
</head>
<body>

    <button id="button">发送 ajax 请求</button>
    <script>
        const button = document.querySelector("#button");
        const onClickGetData = function (e) {
            // ajax 四部曲
            // 创建 xhr 实例
            const xhr = new XMLHttpRequest();
            // 打开网络连接
            xhr.open('GET', 'http://localhost:3001/getData', true);
            xhr.setRequestHeader("name", "FruitJ");
            xhr.withCredentials = true;
            // 设置 xhr 监听
            xhr.addEventListener('readystatechange', function() {
                if(this.readyState === 4) {
                    if((this.status >= 200 && this.status < 300) || this.status === 302) {
                        console.log("结果::", xhr.response, xhr.getResponseHeader("Connection"));
                    }
                }
            }, false);
            // 发送请求
            xhr.send();
        };
        button.addEventListener('click', onClickGetData, false);
    </script>
</body>
</html>